package org.sxp.common.aspect;



import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.sxp.common.constants.DataFilterConstants;
import org.sxp.common.dto.DataAuthorityDTO;
import org.sxp.common.web.XpContextHolder;
import org.sxp.modules.sys.entity.SysUserEntity;
import org.sxp.modules.sys.shiro.ShiroUtils;

/**
 * 数据过滤，自动取hsaf上下文用户对象
 * @Author Administrator
 * @Date 2021/1/21
 */
@Slf4j
public class ControllerDataFilterAspect {

    public void dataFilterBefore(JoinPoint point) throws Throwable {
        // 为了避免web线程被重复使用，控制类aop，每次进来先把权限过滤的数据清除
        DataFilterAspect.remove();

        SysUserEntity currentUser = ShiroUtils.getUserEntity();
        if(currentUser == null){
            log.info("ShiroUtils没有获取到用户信息!!!");
        }else{
            DataAuthorityDTO dataAuthorityDTO = new DataAuthorityDTO();
            dataAuthorityDTO.setUserId(currentUser.getUserId());
            dataAuthorityDTO.setDeptId(currentUser.getDeptId());

            log.debug("控制类数据权限切面-自动放入用户权限条件："+ JSON.toJSONString(dataAuthorityDTO));
            // 放入本地线程上下文，后续直接取
            XpContextHolder.getContext().addProperty(DataFilterConstants.DATA_AUTHORITY_KEY, dataAuthorityDTO);
        }
    }
}
